package com.wc.算法提高课.E第五章_数学知识.高斯消元.开关问题;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.StringTokenizer;

/**
 * @Author congge
 * @Date 2024/10/9 21:54
 * @description https://www.acwing.com/problem/content/description/210/
 */
public class Main {
    /**
     * 思路；<p>
     * 初始状态与结束的状态是否变化时重点, 可以想到异或的高斯消元<p>
     * 当第 j 个灯, 可以被第 i 个开关控制的时候 a[i][j] = 1
     */
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int N = 35;
    static int[][] a = new int[N][N];
    static int n;

    public static void main(String[] args) {
        int T = sc.nextInt();
        while (T-- > 0) {
            n = sc.nextInt();
            for (int i = 1; i <= n; i++) Arrays.fill(a[i], 1, n + 2, 0);
            for (int i = 1; i <= n; i++) {
                a[i][n + 1] = sc.nextInt();
                // 他可以控制自己
                a[i][i] = 1;
            }
            // 只关心开关变化
            for (int i = 1; i <= n; i++) {
                int t = sc.nextInt();
                a[i][n + 1] ^= t;
            }
            int x, y;
            while (true) {
                x = sc.nextInt();
                y = sc.nextInt();
                if (x == 0 && y == 0) break;
                a[y][x] = 1;
            }
            int t = gauss();
            if (t == -1) out.println("Oh,it's impossible~!!");
            else out.println(t);
        }
        out.flush();
    }

    static int gauss() {
        int r, c;
        for (r = 1, c = 1; c <= n; c++) {
            // 主元
            int t = r;
            for (int i = r + 1; i <= n; i++) {
                if (a[i][c] == 1) t = i;
            }
            if (a[t][c] == 0) continue;
            // 交换
            swap(a, r, t);
            // 消除
            for (int i = r + 1; i <= n; i++) {
                for (int j = n + 1; j >= c; j--) {
                    a[i][j] ^= a[i][c] & a[r][j];
                }
            }
            r++;
        }
        int res = 1;
        if (r <= n) {
            for (int i = r; i <= n; i++) {
                if (a[i][n + 1] == 1) return -1;
                res <<= 1;
            }
        }
        return res;
    }

    static void swap(int[][] a, int i, int j) {
        int[] tmp = a[i];
        a[i] = a[j];
        a[j] = tmp;
    }
}

class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    // 是否由下一个
    boolean hasNext() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                String line = br.readLine();
                if (line == null)
                    return false;
                st = new StringTokenizer(line);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }
}